package com.yao.sdk.log;

import java.io.File;
import java.text.SimpleDateFormat;

import com.yao.sdk.common.SdkConstant;
import com.yao.sdk.util.Utils;

import java.util.Date;

import android.util.Log;

/**
 *  Log 工具类
 *
 */
public final class LogUtil {


    public static String printCurTime(long time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm分ss秒");
        return simpleDateFormat.format(new Date(time));
    }

	public static class Config {

        public static final Config INSTANCE = new Config();

		private boolean isOpenSwitch = false; 
		
		private boolean isLogFileExists = false;
		
		private boolean isOpenLogFileSwitch = false;
		
		private String logFilePath = null;
		
		private String defaultTag = null;

        private Config() {}

        public static Config getInstance() {
            return INSTANCE;
        }

		/**
		 * 全局切换Log开关，是否打印log
		 */
	/*	public Config logSwitch(Switch isOpenSwitch){
			 this.isOpenSwitch = isOpenSwitch.booleanValue();
			 return this;
		}*/
		
		/**
		 *  设置默认标签，这种主要用于LogUtil.d() 这种默认一个大标签，通过子标签来区分内容的
		 */
		public Config defaultTag(String defaultTag) {
			this.defaultTag = defaultTag;
			return this;
		}
		
		/**
		 * 线上apk可以通过包路径或者sdcard路径下是否有某个文件来决定是否打开  
		 */
	/*	public Config logFileSwitch(Switch isOpenLogFileSwitch) {
			this.isOpenLogFileSwitch = isOpenLogFileSwitch.booleanValue();
			return this;
		}*/
		/**控制log开关的file的path */
		public Config logFilePath(String logFilePath) {
			this.logFilePath = logFilePath;
            checkLogFilePath();
			return this;
		}
		
	}
	

	public static Config Config() {
		return Config.getInstance();
	} 

	
	/**检查文件是否存在 */
	public static void checkLogFilePath() {
		boolean flag = false;
		try {
			File f = new File(Config.getInstance().logFilePath);
			if ( f != null  ) {
				flag = f.exists();
			}
		} catch (Exception e) { 
			e(SdkConstant.TAG_MAIN, SdkConstant.TAG_LOG, "LogUtil#checkLogFilePath()  errors!!  filePath=" + Config.getInstance().logFilePath);
			flag = false;
			e.printStackTrace();
		}
        Config.getInstance().isLogFileExists = flag;
	}
	
	/**是否打开log的总方法*/
	private static boolean checkLogSwitch() {
		if ( Config.getInstance().isOpenSwitch ) {
			return true;
		} else if (!Config.getInstance().isOpenLogFileSwitch) {
			return false;
		} else if ( !Config.getInstance().isLogFileExists) {
			return false;
		}
		return true;
	}
	
	
	public static void debug(String tag, String msg) {
		log(Log.DEBUG,tag,msg);
	}
	
	public static void verbose(String tag, String msg) {
		log(Log.VERBOSE,tag,msg);
	}
	
	public static void info(String tag, String msg) {
		log(Log.INFO,tag,msg);
	}
	
	public static void error(String tag, String msg) {
		log(Log.ERROR,tag,msg);
	}
	
	public static void warn(String tag, String msg) {
		log(Log.WARN,tag,msg);
	}
	
	private static void log(int level, String tag, String msg) {
		if( !checkLogSwitch() ) {
			return ;
		}
		switch (level) {
		case Log.DEBUG:
			Log.d(tag,  msg);
			break;
		case Log.INFO:
			Log.i(tag, msg);
			break;
		case Log.WARN:
			Log.w(tag,msg);
			break;
		case Log.ERROR:
			Log.e(tag, msg);
			break;
		case Log.VERBOSE:
			 Log.v(tag, msg);
			 break;
		}
	}

    private static void log(int level, String tag, String msg, Throwable throwable) {
        if( !checkLogSwitch() ) {
            return ;
        }
        switch (level) {
            case Log.DEBUG:
                Log.d(tag,  msg, throwable);
                break;
            case Log.INFO:
                Log.i(tag, msg, throwable);
                break;
            case Log.WARN:
                Log.w(tag, msg, throwable);
                break;
            case Log.ERROR:
                Log.e(tag, msg, throwable);
                break;
            case Log.VERBOSE:
                Log.v(tag, msg, throwable);
                break;
        }
    }
	
	public static void d(String subTag, String msg) {
		log2(null, Log.DEBUG,subTag,msg);
	}
	
	public static void v(String subTag, String msg) {
		log2(null, Log.VERBOSE,subTag,msg);
	}
	
	public static void i(String subTag, String msg) {
		log2(null, Log.INFO,subTag,msg);
	}
	
	public static void e(String subTag, String msg) {
		log2(null, Log.ERROR,subTag,msg);
	}
	
	public static void w(String subTag, String msg) {
		log2(null, Log.WARN,subTag,msg);
	}
	
	public static void d(String mainTag, String subTag, String msg) {
		log2(mainTag, Log.DEBUG,subTag,msg);
	}
	
	public static void v(String mainTag, String subTag, String msg) {
		log2(mainTag, Log.VERBOSE,subTag,msg);
	}
	
	public static void i(String mainTag, String subTag, String msg) {
		log2(mainTag, Log.INFO,subTag,msg);
	}
	
	public static void e(String mainTag, String subTag, String msg) {
		log2(mainTag, Log.ERROR,subTag,msg);
	}

    public static void e(String mainTag, String subTag, Throwable ex) {
        log(Log.ERROR, mainTag, subTag, ex);
    }
	
	public static void w(String mainTag, String subTag, String msg) {
		log2(mainTag, Log.WARN,subTag,msg);
	}
	
	private static void log2(String mainTag, int level, String subTag, String msg) {
		if( !checkLogSwitch() ) {
			return ;
		}
		String tag = null;
		if ( Utils.isEmpty(Config.getInstance().defaultTag) && Utils.isEmpty(mainTag) ) {
			 return;
		} else if ( !Utils.isEmpty(mainTag)){
			tag = mainTag;
		} else {
			tag = Config.getInstance().defaultTag;
		}
		if ( Utils.isEmpty(subTag) ) {
			subTag = "unknowned";
		}
		switch (level) {
		case Log.DEBUG:
			Log.d(tag,  subTag + ": " + msg);
			break;
		case Log.INFO:
			Log.i(tag,  subTag + ": " + msg);
			break;
		case Log.WARN:
			Log.w(tag, subTag + ": " + msg);
			break;
		case Log.ERROR:
			Log.e(tag,  subTag + ": " + msg);
			break;
		case Log.VERBOSE:
			 Log.v(tag,  subTag + ": " + msg);
			 break;
		}
	}
	
	
	
}
